
module.exports = class extends think.cmswing.ws {
  // 构造方法
  constructor(ctx) {
    super(ctx);
  }
  async __before() {
    await super.__before();
  }
  indexAction() {
    let data = {};
    data.userActive = {
      '描述': '注册通过后回写',
      '接收参数': 'user对象的若干属性',
      '返回参数': 'errno 错误码；errmsg 错误信息；data 返回数据；',
    };
    return this.json(data);
  }

  // 测试用请求
  async testAction() {
    // NC查询示例
    /*
    const params = {
      sql: 'select * from bd_corp',
    };
    const data = await this.requestNC('executeQuery', params);
    */
    /*
    // OA查询示例
    const params = {
      sql: 'select * from formtable_main_89',
    };
    const data = await this.requestOA('executeQuery', params);
    */
    // const data = await this.request('http://localhost:8360/ws/portal/userActive',params);

    const list = await think.model('cmswing/type').getGsName(1);
    console.log('++++++++++++++res++++++++++++++++'+JSON.stringify(list));

    return this.json(list);
  }

  /**
   * 注册通过后回写触发订单审核流程
   */
  async userActiveAction() {
    try {
      const data = this.getData();
      let resp;
      let resdata;
      console.log('data='+data);
      if (think.isEmpty(data)) return this.fail('参数不能为空');
      if (!data.user_id) {
        return this.fail('该用户在门户中不存在，请联系管理员！');
      } else {
        const user_id = data.user_id;
        const use = await this.model('member').where({id: data.user_id}).find();
        if (!use) {return this.fail('根据id（' + data.user_id + '）获取到的用户为空');}
        const order_id = await this.model('usertoorder').where({user_id:user_id}).getField('order_id',true);
        if (!order_id) {
          return this.fail('该用户不存在用户订单关系，请联系管理员！');
        }
        const order = await this.model('order').where({id:order_id}).find();
        if (!order) {
          return this.fail('未找到对应订单，请联系管理员！');
        }
        let memberdata = await this.model('member').where({id:order.gongid}).find();
        let memberdata2 = await this.model('member').where({id:order.xuid}).find();
        let action = '';
        if (memberdata.groupid ==12&&memberdata2.groupid !=12) {
          memberdata2.yewuyuan = memberdata.ygbh;
          const gsmcid = await this.model('zzjg').where({zzjg:order.gong_gsmc}).getField('id',true);
          if (think.isEmpty(gsmcid)) {
            return this.fail('核心企业业务员公司名称有误，请先完善用户信息。');
          }
          memberdata2.company = gsmcid;
          action = 'SaleOrder';
          order.user_info = memberdata2;
          try {
            resp = await this.requestOA(action, order);
            if (resp.errno !== 0) {//调用接口失败
              console.log('4+++++++++++++++++++++++++++++++++++++++++++');
              return this.fail(resp.errmsg + '[WS]');
            }else {
              console.log('5+++++++++++++++++++++++++++++++++++++++++++');
            }
          } catch (e) {
            console.log('6+++++++++++++++++++++++++++++++++++++++++++');
            return this.fail('销售合同生成失败，请重试。[WS]');
          }
        }else if (memberdata.groupid !=12&&memberdata2.groupid ==12) {
          memberdata.yewuyuan = memberdata2.ygbh;
          const gsmcid = await this.model('zzjg').where({zzjg:order.xu_gsmc}).getField('id',true);
          if (think.isEmpty(gsmcid)) {
            return this.fail('核心企业业务员公司名称有误，请先完善用户信息。');
          }
          memberdata.company = gsmcid;
          action = 'CaigouOrder';
          order.user_info = memberdata;
          try {
            resp = await this.requestOA(action, order);
            if (resp.errno !== 0) {//调用接口失败
              console.log('10+++++++++++++++++++++++++++++++++++++++++++');
              return this.fail(resp.errmsg + '[WS]');
            }else {
              resdata = resp;
              /*const memberres2 = await this.model('member').where({id:order.xuid}).update({oastatus:1});*/
              console.log('11+++++++++++++++++++++++++++++++++++++++++++');
            }
          } catch (e) {
            console.log('12+++++++++++++++++++++++++++++++++++++++++++');
            return this.fail('采购合同生成失败，请重试。[WS]');
          }
        }
      }
      const resmem = await this.model('member').where({id: data.user_id}).update({oastatus: 2});
    } catch (e) {
      if (e.sqlState == '23000') {
        return this.fail('客户已存在，不能再次创建，请检查。');
      } else {
        return this.fail(JSON.stringify(e));
      }
    }
    return this.success('合同回写成功');
  }
  /**
   * 合同审批完成回写订单
   */
  async orderActiveAction() {
    try {
      const data = this.getData();
      think.logger.info('orderActiveAction:data', data);
      if (think.isEmpty(data)) return this.fail('参数不能为空');
      const portalid = data.portalid;
      const htbh = data.htbh;
      if (!portalid) {
        return this.fail('该合同门户中不存在，请联系管理员！');
      }
      if (!htbh) {
        return this.fail('该合同无合同编号，请联系管理员！');
      }
      const order = await this.model('order').where({order_no: portalid}).find();
      if (!order) {return this.fail('根据订单编号（' + portalid + '）获取到的订单为空');}
      const resmem = await this.model('order').where({order_no: portalid}).update({htbh:htbh,status: 3,remark3:'OA合同审批通过，自动完成订单'});
      if (!resmem) {return this.fail('订单修改失败');}


    } catch (e) {
      if (e.sqlState == '23000') {
        return this.fail('客户已存在，不能再次创建，请检查。');
      } else {
        return this.fail(JSON.stringify(e));
      }
    }
    return this.success('用户回写成功');
  }
};
